package com.huanhuan.index;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;

public class ESTest_Client_Index_Create {

    public static void main(String[] args) throws Exception{
        //1.创建客户端对象
        //创建服务主机名 和 Web 通信端口
        final String hostname = "localhost";
        final int port = 9200;
        final String scheme = "http";
        HttpHost httpHost = new HttpHost( hostname, port, scheme );

        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(httpHost)
        );

        //2.创建索引
        CreateIndexRequest request = new CreateIndexRequest( "huan" );
        CreateIndexResponse createIndexResponse =
                client.indices().create( request, RequestOptions.DEFAULT );

        //查看是否响应
        boolean acknowledged = createIndexResponse.isAcknowledged();

        System.out.println("es创建索引 : " + acknowledged);


        //关闭ES 资源
        client.close();
    }


}
